package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

/**
 * Created by QIUzh on 2020/11/21 17:22
 */
@RestController
@RequestMapping("/setting")
public class SettingController {
    @Reference
    private SettingService settingService;

/*
* 角色管理*/
@RequestMapping(value = "/findPage4role",method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
    try {
        return settingService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
    /**
     * 新增角色
     */

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody Role role) {
        try {
            int count=settingService.queryByname(role.getName(),role.getKeyword());

            if(count!=0){
                return new Result(false,MessageConstant.ADD_ROLE_FAIL);
            }

            settingService.add(role);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FAIL);
        }
    }
//删除

    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    public Result deleteById(Integer id){
        try {
            settingService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }
    /**
     * 编辑
     */

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Result edit(@RequestBody Role role){
        try {
            settingService.edit(role);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }
    /**
     * 根据id查询角色数据
     */
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(Integer id){
        try {
           Role role = settingService.findById(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS ,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    @RequestMapping(value = "/findPermissionById",method = RequestMethod.GET)
    public  List<Integer>findGroupIdsBySetmealId(Integer id){
        try {
            return settingService.findPermissionById (id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



    @RequestMapping(value ="/findPermissionAll",method = RequestMethod.GET)
    public  List<Permission> findPermissionAll(){
        try {
            return settingService.findPermissionAll ();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @RequestMapping(value = "/findPrIdsByid",method = RequestMethod.GET)
    public List<Integer> findPrIdsByid(Integer id){
        try {
            return settingService.findPrIdsByid (id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping(value ="/findMenuAll",method = RequestMethod.GET)
    public  List<Menu> findMenuAll(){
        try {
            return settingService.findMenuAll ();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @RequestMapping(value = "/findMnIdsByid",method = RequestMethod.GET)
    public List<Integer> findMnIdsByid(Integer id){
        try {
            return settingService.findMnIdsByid (id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @RequestMapping(value = "/fenPei",method = RequestMethod.POST)
    public Result fenPei(@RequestBody Role role, Integer[] checkIds) {
        try {
            settingService.fenPei(role,checkIds);
            return new Result(true,MessageConstant.FENPEI_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.FENPEI_FAIL);
        }
    }

    @RequestMapping(value = "/fenPei2",method = RequestMethod.POST)
    public Result fenPei2(@RequestBody Role role, Integer[] checkIds2) {
        try {
            settingService.fenPei2(role,checkIds2);
            return new Result(true,MessageConstant.FENPEI_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.FENPEI_FAIL);
        }
    }
}
